/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.marcelo.agro.productos.gui;

import java.awt.BorderLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import org.marcelo.agro.core.DynamicLookup;
import org.marcelo.agro.entidades.Productos;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//org.marcelo.agro.productos.gui//AgregarProductos//EN",
autostore = false)
@TopComponent.Description(preferredID = "AgregarProductosTopComponent",
//iconBase="SET/PATH/TO/ICON/HERE", 
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "flotante", openAtStartup = false)
@ActionID(category = "Window", id = "org.marcelo.agro.productos.gui.AgregarProductosTopComponent")
@ActionReference(path = "Menu/Registros" /*
 * , position = 333
 */)
@TopComponent.OpenActionRegistration(displayName = "#CTL_AgregarProductosAction",
preferredID = "AgregarProductosTopComponent")
@Messages({
    "CTL_AgregarProductosAction=AgregarProductos",
    "CTL_AgregarProductosTopComponent=AgregarProductos Window",
    "HINT_AgregarProductosTopComponent=This is a AgregarProductos window"
})
public final class AgregarProductosTopComponent extends TopComponent {

    public AgregarProductosTopComponent() {
        initComponents();
        setName(Bundle.CTL_AgregarProductosTopComponent());
        setToolTipText(Bundle.HINT_AgregarProductosTopComponent());
        putClientProperty(TopComponent.PROP_DRAGGING_DISABLED, Boolean.TRUE);
        putClientProperty(TopComponent.PROP_MAXIMIZATION_DISABLED, Boolean.TRUE);
        putClientProperty(TopComponent.PROP_SLIDING_DISABLED, Boolean.TRUE);
        setLayout(new BorderLayout());

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    AgregarProductos ap;
    @Override
    public void componentOpened() {
        // TODO add custom code on component opening
        Productos producto = DynamicLookup.getDefault().lookup(Productos.class);
        ap = new AgregarProductos(producto);
        add(ap,BorderLayout.CENTER);
        ap.addPropertyChangeListener(listener);
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
        remove(ap);
        ap.removePropertyChangeListener(listener);
        ap = null;
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }
    
    private PropertyChangeListener listener = new PropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if (evt.getPropertyName().equals(AgregarProductos.PROPIEDAD_SALIR) ){
                close();  
            }
        }
    };
}
